const express = require("express")
const fs = require("fs")
const path = require("path")
const route = express.Router()

//引入权限控制校验中间件
const isLoginMiddel = require("../middleware/isLoginMiddel")

//引入数据库集合
const addAdvModel = require("../db/model/addAdvModel")
route.delete("/deleteAdv",isLoginMiddel,async (req, res) => {
   const{
       _id
   }=req.query


   //删除文件
  const findResult =await  addAdvModel.findOne({_id})
  const href= findResult.advHref.split("/")[2]
  fs.unlink(path.resolve(__dirname,"../upload",href),()=>{
    console.log("删除文件成功");
  })


   //删除广告数据
  try {
    await addAdvModel.deleteOne({_id})
    res.send({
        code:10000,
        msg:"ok",
        data:null
    })
  } catch (e) {
      res.send({
          code:10001,
          msg:"数据库删除广告异常"+e.message,
          data:null
      })
  }
})

module.exports = route